package com.dxd.servlet.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.constant.ErrorCodeConstant;
import com.dxd.entity.ReturnBean;
import com.dxd.entity.UserInfo;
import com.dxd.servlet.PublicServlet;

/**
 * 后端用户管理
 * 
 * @author pppcar
 *
 */
@WebServlet("/user/userInfo")
public class UserInfoServlet extends PublicServlet {
	private static final long serialVersionUID = -2754603844053914575L;
	private static final Logger LOG = LoggerFactory.getLogger(UserInfoServlet.class);

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("type");
		if (type == null || "".equals(type) || "goLogin".equals(type)) {// 跳转到登录页面
			LOG.info("跳转登录页面，type：{},url:{}", type, request.getContextPath());
			Dispatcher("/view/admin/login.jsp");
		} else if ("login".equals(type)) {
			login(request);// 登录请求
			return;
		} else if ("loginOut".equals(type)) {
			LOG.info("退出系统，清空session");
			getSession().setAttribute("loginSession", null);
			getSession().setAttribute("userId", null);
			Dispatcher("/view/admin/login.jsp");
		} else if ("upload".equals(type)) {
			Dispatcher("/view/ajaxupload.jsp");
		}else if("goMyInfo".equals(type)){
			UserInfo userInfo=getUserInfo();
			request.setAttribute("user", userInfo);
			Dispatcher("/view/admin/admin_info.jsp");
		}else if("updatePassWord".equals(type)){
			ReturnBean returnBean=ReturnBean.returnBeanBuild();
			String passWord=request.getParameter("passWord");
			String newPassWord=request.getParameter("newPassWord");
			UserInfo user=getUserInfo();
			if(user==null||user.getId()==null){
				Redirect(request.getContextPath() + "/user/userInfo");
			}
			if(!user.getPassWord().equals(passWord)){
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("原密码不正确");
				returnJson(returnBean);
				return;
			}
			user.setPassWord(newPassWord);
			userInfoDao.updateUser(user);
			returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("修改成功");
			returnJson(returnBean);
			return;
		}
	}

	/**
	 * 登录验证
	 * 
	 * @param request
	 * @param returnMap
	 */
	private void login(HttpServletRequest request) {
		ReturnBean returnBean = ReturnBean.returnBeanBuild();
		String userName = request.getParameter("userName");// 登录用户名
		String passWord = request.getParameter("passWord");// 密码
		if (userName == null || "".equals(userName)) {
			returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("用户名为空");
			returnJson(returnBean);
			return;
		}
		if (passWord == null || "".equals(passWord)) {
			returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("密码为空");
			returnJson(returnBean);
			return;
		}
		UserInfo userInfo = userInfoDao.Login(userName, passWord, 1);
		if (userInfo != null && userInfo.getUserName() != null) {
			// 验证成功
			HttpSession session = request.getSession();
			session.setAttribute("loginSession", userName);
			session.setAttribute("userId", userInfo.getId());
			// 设置登录小时失效
			session.setMaxInactiveInterval(60 * 60 * 3);
			returnBean.setReturnCode(ErrorCodeConstant.SUCCESS).setReturnMsg("登录成功空");
			returnJson(returnBean);
			return;
		} else {
			// 验证失败
			returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("用户名或密码错误");
			returnJson(returnBean);
			return;
		}
	}

}